package net.kazed.nextaction.context.activity;

import net.kazed.nextaction.ApplicationResource;
import net.kazed.nextaction.NextActionApplication;
import net.kazed.nextaction.R;
import net.kazed.nextaction.database.Task;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class InboxActivity extends ListActivity {

   private static final int NEW_TASK = 1;
   private static final int VIEW_TASK = 2;
   
   private ApplicationResource applicationResource;

   private TaskListDisplayMenu taskListDisplayMenu;

	@Override
    public void onCreate(Bundle icicle) {
      NextActionApplication application = (NextActionApplication) getApplication();
      applicationResource = application.getApplicationResource();
	      
      setContentView(R.layout.inbox);
      setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);
      
      taskListDisplayMenu = new TaskListDisplayMenu(this);

      setCursor();
      
      super.onCreate(icicle);
	}
		
	@Override
	protected void onResume() {

		super.onResume();
	}

    @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
      Uri itemUri = ContentUris.withAppendedId(Task.CONTENT_URI, id);

      String action = getIntent().getAction();
      if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {
         // The caller is waiting for us to return a task selected by
         // the user. They have clicked on one, so return it now.
         Intent intent = new Intent();
         setResult(RESULT_OK, intent);
      } else {
         // Launch activity to view/edit the currently selected item
         Intent intent = new Intent(Intent.ACTION_VIEW, itemUri);
         startActivityForResult(intent, VIEW_TASK);
      }
   }

    @Override
   public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the currently selected menu XML resource.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.inbox, menu);
        
        taskListDisplayMenu.onCreateOptionsMenu(menu);
        
      return super.onCreateOptionsMenu(menu);
   }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
       case R.id.menu_insert_task:
          Uri itemUri = getContentResolver().insert(Task.CONTENT_URI, null);
          Intent intent = new Intent(Intent.ACTION_INSERT, itemUri);
          startActivityForResult(intent, NEW_TASK);
          break;
       default:
           boolean displayChanged = taskListDisplayMenu.onOptionsItemSelected(item);
           if (displayChanged) {
               setCursor();
           }
           break;
       }

       return super.onOptionsItemSelected(item);
    }
    
    /**
     * Set query cursor.
     */
    private void setCursor() {
        String where = null;
        String displayWhere = taskListDisplayMenu.getWhereClause();
        if (displayWhere == null) {
            where = Task.INBOX_SELECTION;
        } else {
            where = Task.INBOX_SELECTION + " and " + displayWhere;
        }
        Cursor cursor = managedQuery(Task.CONTENT_URI, Task.FULL_PROJECTION.getFieldNames(), where,
                        null, Task.SORT_ORDER);
        setListAdapter(new TaskListAdapter(this, cursor, applicationResource));

    }

}
